from paste.httpexceptions import HTTPFound
from zope.interface import implements

from repoze.who.interfaces import IMetadataProvider
from repoze.who.interfaces import IAuthenticator

from repoze_who.model.user import User


class UserModelPlugin(object):
    
    implements(IAuthenticator, IMetadataProvider)
     
    # IAuthenticator
    def authenticate(self, environ, identity):
        try:
            username = identity['login']
            password = identity['password']
        except KeyError:
            return None
        
        #success = User.authenticate(username, password)
        id = str(123456)
        if username == password == "":
            return None
            id = str(666)
        #environ['repoze.who.identity'] = id
        return id

    # IMetadataProvider
    def add_metadata(self, environ, identity):
        userid = identity.get('repoze.who.userid')
        user = User(int(userid))
        if user is not None:
            identity['user'] = user
            environ["REMOTE_USER"] = user